package com.xl.rentkeeper.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.squareup.wire.Wire;
import com.xl.fuse.protos.viewroom.ViewOrder;
import com.xl.rentkeeper.R;
import com.xl.rentkeeper.entity.Order;
import com.xl.rentkeeper.msg.Message;
import com.xl.rentkeeper.util.TimeUtils;

import java.util.List;

/**
 * Created by Administrator on 2015/7/20.
 */
public class PushAdapter extends BaseAdapter {
    Context context;
    List<Message> list;
    Wire wire = new Wire();
    public PushAdapter(Context c,List<Message> list){
        this.context = c;
        this.list=list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView tv_push;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.push_item, null);
            tv_push = (TextView) convertView.findViewById(R.id.tv_push);
            convertView.setTag(tv_push);
        }else{
            tv_push = (TextView) convertView.getTag();
        }
        Message msg = list.get(position);
//        msg.msg.msgBody.
        String content;
        if(!TextUtils.isEmpty(msg.netState)){
            content ="网络变更为："+msg.netState+" "+ TimeUtils.fmtDate(msg.msgTime*1000);
        }else{
            content = ""+msg.seq+" generate "+ TimeUtils.fmtDate(msg.msgTime)+"\nreceive"+TimeUtils.fmtDate(msg.receivingTime);
        }
        try {
            if (msg.msg != null && msg.msg.msgHead != null) {
                content = content + "\n"+msg.msg.msgHead.toString();
                if (msg.msg.msgHead.msgType == 1) {
                    byte[] bytes = msg.msg.msgBody.context.toByteArray();
                    try {
                        ViewOrder viewOrder = wire.parseFrom(bytes, ViewOrder.class);
                        content = content + "\n"+viewOrder.toString();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        tv_push.setText(content);
        return convertView;
    }
}
